import random
import time


def bubble_sort(a, num):
    flag = 0
    for i in range(num - 1):
        for j in range(num - i - 1):
            if a[j] > a[j + 1]:
                a[j], a[j + 1] = a[j + 1], a[j]
                flag = 1
        if flag == 0:
            break


def opt_sort(a, num):
    min = 0
    for i in range(num):
        min = i
        for j in range(i + 1, num):
            if a[j] < a[min]:
                min = j
        if min != i:
            a[i], a[min] = a[min], a[i]


num = int(input('请输入列表长度'))
beg_time = time.time()
a = [random.randint(1, num) for i in range(num)]
print(f'原列表是{a}')
opt_sort(a, num)
end_time = time.time()
run_time = end_time - beg_time
print(a)
print(f'选择排序时间为{run_time}秒')


beg_time = time.time()
a = [random.randint(1, num) for j in range(num)]
print(f'原列表是{a}')
bubble_sort(a, num)
end_time = time.time()
run_time = end_time - beg_time
print(a)
print(f'冒泡排序时间是{run_time}秒')



